﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Net.Mime;
using DAL;
using System.Collections.Generic;
using System.ComponentModel;
using Telerik.Web.UI;
using System.Globalization;



public partial class Cost_UserControl_uc_ChiPhi_NhapHang_TraCuu : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            InitData();
        }
    }

    private void InitData()
    {
        lblAlerting.Text = string.Empty;
        txtTuNgay.Text = DateTime.Now.AddDays(-15).ToString("dd/MM/yyyy");
        txtDenNgay.Text = DateTime.Now.AddDays(5).ToString("dd/MM/yyyy");
        hdfTuNgay.Value = txtTuNgay.Text.Trim();
        hdfDenNgay.Value = txtDenNgay.Text.Trim();
        DateTime dtTuNgay = new DateTime();
        DateTime dtDenNgay = new DateTime();
        try
        {
            dtTuNgay = DateTime.ParseExact(txtTuNgay.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
            dtDenNgay = DateTime.ParseExact(txtDenNgay.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
            hdfTuNgay.Value = txtTuNgay.Text.Trim();
            hdfDenNgay.Value = txtDenNgay.Text.Trim();
        }
        catch
        {
            lblAlerting.Text = "Bạn chưa nhập ngày hoặc ngày không đúng!";
            return;
        }
        CostBO pur = new CostBO();
        List<ZD_PRC_ZD_COST_SEARCHResult> result = new List<ZD_PRC_ZD_COST_SEARCHResult>();
        result = pur.Cost_Search(dtTuNgay, dtDenNgay, txtSoVanDon.Text.Trim());
        grdList.DataSource = result;
        grdList.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        lblAlerting.Text = string.Empty;
        DateTime dtTuNgay = new DateTime();
        DateTime dtDenNgay = new DateTime();
        try
        {
            dtTuNgay = DateTime.ParseExact(txtTuNgay.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
            dtDenNgay = DateTime.ParseExact(txtDenNgay.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
            hdfTuNgay.Value = txtTuNgay.Text.Trim();
            hdfDenNgay.Value = txtDenNgay.Text.Trim();
        }
        catch
        {
            lblAlerting.Text = "Bạn chưa nhập ngày hoặc ngày không đúng!";
            return;
        }
        CostBO pur = new CostBO();
        List<ZD_PRC_ZD_COST_SEARCHResult> result = new List<ZD_PRC_ZD_COST_SEARCHResult>();
        result = pur.Cost_Search(dtTuNgay, dtDenNgay, txtSoVanDon.Text.Trim());
        grdList.DataSource = result;
        grdList.DataBind();
    }


    protected void grdList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdList.PageIndex = e.NewPageIndex;
        DateTime dtTuNgay = DateTime.ParseExact(hdfTuNgay.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture);
        DateTime dtDenNgay = DateTime.ParseExact(hdfDenNgay.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture);

        CostBO pur = new CostBO();
        List<ZD_PRC_ZD_COST_INPUT_SEARCHResult> result = new List<ZD_PRC_ZD_COST_INPUT_SEARCHResult>();
        result = pur.Cost_Input_Search(dtTuNgay, dtDenNgay, txtSoVanDon.Text.Trim());
        grdList.DataSource = result;
        grdList.DataBind();
    }

    // Lay datatable phi
    private DataTable ConvertToDataTable<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult>(List<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

    // Lay master
    private DataTable ConvertToDataTableMaster<ZD_PRC_RPT_ZD_COST_MASTERResult>(List<ZD_PRC_RPT_ZD_COST_MASTERResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(ZD_PRC_RPT_ZD_COST_MASTERResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (ZD_PRC_RPT_ZD_COST_MASTERResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

    // Lay detail
    private DataTable ConvertToDataTableDetail<ZD_PRC_RPT_ZD_COST_DETAILResult>(List<ZD_PRC_RPT_ZD_COST_DETAILResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(ZD_PRC_RPT_ZD_COST_DETAILResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (ZD_PRC_RPT_ZD_COST_DETAILResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

    // Lay detailGroup
    private DataTable ConvertToDataTableDetailGroup<ZD_PRC_RPT_ZD_COST_DETAIL_GROUPResult>(List<ZD_PRC_RPT_ZD_COST_DETAIL_GROUPResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(ZD_PRC_RPT_ZD_COST_DETAIL_GROUPResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (ZD_PRC_RPT_ZD_COST_DETAIL_GROUPResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }
    protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "View")
        {
            lblAlerting.Text = string.Empty;
            int index = Convert.ToInt32(e.CommandArgument);
           
            OutReport outRPT = new OutReport();
            //lay database
            CostBO pur = new CostBO();
            //lay phi group 1
            List<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult> rscost1 = new List<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult>();
            rscost1 = pur.rpt_getPhiByGroupId(index, 1);
            DataTable dtcost1 = ConvertToDataTableMaster(rscost1);

            //lay phi group 2
            List<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult> rscost2 = new List<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult>();
            rscost2 = pur.rpt_getPhiByGroupId(index, 2);
            DataTable dtcost2 = ConvertToDataTableMaster(rscost2);

            //lay phi group 3
            List<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult> rscost3 = new List<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult>();
            rscost3 = pur.rpt_getPhiByGroupId(index, 3);
            DataTable dtcost3 = ConvertToDataTableMaster(rscost3);

            //master
            List<ZD_PRC_RPT_ZD_COST_MASTERResult> rsmaster = new List<ZD_PRC_RPT_ZD_COST_MASTERResult>();
            rsmaster = pur.rpt_getMasterByMasterId(index);
            DataTable dtmaster = ConvertToDataTableMaster(rsmaster);

            //detail group
            List<ZD_PRC_RPT_ZD_COST_DETAIL_GROUPResult> rsdetailGroup = new List<ZD_PRC_RPT_ZD_COST_DETAIL_GROUPResult>();
            rsdetailGroup = pur.rpt_getDetailGroupByMasterId(index);
            DataTable dtdetailGroup = ConvertToDataTableDetailGroup(rsdetailGroup);
            //detail
            List<ZD_PRC_RPT_ZD_COST_DETAILResult> rsdetail = new List<ZD_PRC_RPT_ZD_COST_DETAILResult>();
            rsdetail = pur.rpt_getDetailByMasterId(index);
            DataTable dtdetail = ConvertToDataTableDetail(rsdetail);

            // datatable cost
            DataTable ReportCost1 = new DataTable();
            DataTable ReportCost2 = new DataTable();
            DataTable ReportCost3 = new DataTable();
            ReportCost1 = dtcost1;
            ReportCost2 = dtcost2;
            ReportCost3 = dtcost3;
            ReportCost1.TableName = "PHI1";
            ReportCost2.TableName = "PHI2";
            ReportCost3.TableName = "PHI3";
            // datatable master
            DataTable ReportMaster = new DataTable();
            ReportMaster = dtmaster;
            ReportMaster.TableName = "MASTER";
            // datatable detailgroup
            DataTable ReportDetailGroup = new DataTable();
            ReportDetailGroup = dtdetailGroup;
            ReportDetailGroup.TableName = "ReportGroup";
            // datatable detail
            DataTable ReportDetail = new DataTable();
            ReportDetail = dtdetail;
            ReportDetail.TableName = "Report";

            DataTable[] arrTable = { ReportCost1, ReportCost2, ReportCost3, ReportMaster, ReportDetail, ReportDetailGroup };
            outRPT.OutReportToScreen(MapPath("~/RPTTemplate/LANDEDCOST_TEMPLATE.xls"), arrTable, "_SoVanDon_LandedCost.xls");


        }
        if (e.CommandName == "Edit")
        {
            lblAlerting.Text = string.Empty;
            int index = Convert.ToInt32(e.CommandArgument);
            string Link = "../cost/cp_" + index.ToString();
            Response.Redirect(Link);
        }
    }
}
